package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Employee;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class EmployeeDao {
	private DBHelp<Employee> db = new DBHelp<Employee>();

	public Employee findByName(String username) {
		String sql = "select id,username,password,email,pic from t_employee where username = ?";
		return db.executeQueryForObject(sql, new EmployeeRowMapper(), username);
	}
	public List<Employee> findAll() {
		String sql = "select id,username,password,email,pic from t_employee";
		return db.executeQueryForList(sql, new EmployeeRowMapper());
	}
	
	public void save(Employee e) {
		final String sql = "INSERT INTO t_employee(id, username, `password` , email, pic) VALUES(?,?,?,?,?)";
		db.executeSQL(sql, e.getId(), e.getUsername(), e.getPassword(), e.getEmail(), e.getPic());
	}
	
	
	
	private class EmployeeRowMapper implements RowMapper<Employee>{

		public Employee mapRow(ResultSet rs) throws SQLException {
			Employee e = new Employee();
			e.setEmail(rs.getString("email"));
			e.setId(rs.getString("id"));
			e.setPassword(rs.getString("password"));
			e.setPic(rs.getString("pic"));
			e.setUsername(rs.getString("username"));
			return e;
		}
		
	}







}
